/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.TitleBarBorder;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.fireflow.pdl.fpdl20.diagram.GroupShape;
import org.fireflow.pdl.fpdl20.diagram.basic.Circle;
import org.fireflow2.designer.eclipse.modelwrapper.basic.GroupWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class GroupFigure extends RoundedRectangle implements CommonFigureInterface{
	GroupWrapper groupWrapper = null;
	TitleBarBorder border = null;
	Color borderColor = ColorConstants.black;
	public GroupFigure(GroupWrapper poolWrapper){
		this.groupWrapper = poolWrapper;
		this.setLayoutManager(new FreeformLayout());
		this.setCornerDimensions(new Dimension(10,10));
		this.setLineStyle(org.eclipse.swt.SWT.LINE_DASH);
		this.setLineWidth(1);

		border = new MyTitleBarBorder(1);
		border.setPadding(3);
		border.setLabel("");
		border.setTextAlignment(PositionConstants.CENTER);
		border.setTextColor(ColorConstants.black);
		this.setBorder(border);
		this.setOpaque(false);
	}
	
	protected boolean useLocalCoordinates() {
		return true;
	}

	public Rectangle getBox(){
		Rectangle rectangle = new Rectangle(this.getBounds());
		return rectangle;
	}
	
	public Rectangle getSelectionHandleBounds(){
		Rectangle rectangle = new Rectangle(this.getBounds());
		this.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	
	class MyTitleBarBorder extends TitleBarBorder{
		int borderWidth = 1;
		public MyTitleBarBorder(int w){
			borderWidth = w;
		}
		protected Insets calculateInsets(IFigure figure) {
			return new Insets(getTextExtents(figure).height + getPadding().getHeight(),
					borderWidth, borderWidth,borderWidth);
		}
		public boolean isOpaque() {
			return false;
		}
		
		public void paint(IFigure figure, Graphics g, Insets insets) {
			tempRect.setBounds(getPaintRectangle(figure, insets));
			Rectangle rec = tempRect;
			rec.height = Math.min(rec.height, getTextExtents(figure).height
					+ getPadding().getHeight());
			g.clipRect(rec);

			int x = rec.x + getPadding().left;
			int y = rec.y + getPadding().top;

			int textWidth = getTextExtents(figure).width;
			int freeSpace = rec.width - getPadding().getWidth() - textWidth;

			if (getTextAlignment() == PositionConstants.CENTER)
				freeSpace /= 2;
			if (getTextAlignment() != PositionConstants.LEFT)
				x += freeSpace;

			g.setFont(getFont(figure));
			g.setForegroundColor(getTextColor());
			g.drawString(getLabel(), x, y);
		}
	}

	public void refreshFigure(){
		GroupShape poolShape = (GroupShape)groupWrapper.getDiagramElement();
		org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect = 
			(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)poolShape.getShape();
		
		if (!org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT.equals(fireRect.getTitle())){
			border.setLabel(fireRect.getTitle());
		}
		

		//线条颜色
		String color = fireRect.getBounds().getBoundsStyle().getColor();
		if (color==null || color.trim().equals("")){
			color = "#696969";
		}
		borderColor = Util4Eclipse.colorStr2ColorObj(color);

		
		//字体及颜色
	    Color fontColor = Util4Eclipse.colorStr2ColorObj(fireRect.getTitleLabel().getFont().getColor());    
	    String fontName = "Trebuchet MS";
	    Font systemFont = Display.getCurrent().getSystemFont();
	    if (systemFont!=null && systemFont.getFontData().length>0){
	    	FontData fd = systemFont.getFontData()[0];
	    	if (fd.getName()!=null){
	    		fontName = fd.getName();
	    	}		    	
	    }
	    int fontStyle = SWT.NORMAL;
	    if (org.fireflow.pdl.fpdl20.diagram.style.Font.FONT_WEIGHT_BOLD.equals(fireRect.getTitleLabel().getFont().getWeight())){
	    	fontStyle = SWT.BOLD;
	    }
	    Font newFont = new Font(Display.getCurrent(),fontName,fireRect.getTitleLabel().getFont().getSize(),fontStyle);
	    border.setTextColor(fontColor);
	    border.setFont(newFont);
	}
	protected void outlineShape(Graphics graphics){
		Color oldColor = graphics.getForegroundColor();
		graphics.setForegroundColor(borderColor);
		super.outlineShape(graphics);
		graphics.setForegroundColor(oldColor);
	}
	
	protected void fillShape(Graphics graphics) {
//		if (this.isOpaque()){
			graphics.fillRoundRectangle(getBounds(), corner.width, corner.height);
//		}
	}
}
